package com.zdya.combatdocuments.util;

import org.java_websocket.util.Base64;

import java.io.ByteArrayInputStream;
import java.security.KeyStore;

public class SecurityUtils {

	public static void main(String[] args) throws Exception {
		System.out.println(readPfxToKeyStore("D:\\123.pfx","25080334"));
	}
	
	public static KeyStore readPfxToKeyStore(String pfxPath, String pwd) throws Exception {

//		BouncyCastleProvider provider = new BouncyCastleProvider();
//		Security.addProvider(provider);
		KeyStore ks1 = KeyStore.getInstance("PKCS12");
		ByteArrayInputStream is1 = null;
//		byte[] pfxB1 = FileUtil.getContent(pfxPath);
		byte[] pfxB1 = Base64.decode(pfxPath);
		try {
			is1 = new ByteArrayInputStream(pfxB1);
			ks1.load(is1, pwd.toCharArray());
		} catch (Exception e) {

			if (is1 != null) {
				is1.close();
			}
			pfxB1 = Base64.decode(pfxB1);
			is1 = new ByteArrayInputStream(pfxB1);
			ks1.load(is1, pwd.toCharArray());
		} finally {
			if (is1 != null) {
				is1.close();
			}
		}

		return ks1;
	}
}
